/*
 * Copyright (C), 2014-2015, 联创车盟汽车服务有限公司
 * FileName: UserManageController.java
 * Author:   ryan
 * Date:     2015年2月2日 上午9:44:33
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>    <time>    <version>    <desc>
 * 修改人姓名        修改时间        版本号        描述
 */
package com.linkage.controller.background;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.linkage.interceptor.NeedAdminLogin;
import com.linkage.pojo.background.UserRole;
import com.linkage.pojo.consumer.User;
import com.linkage.service.background.UserManageService;

/**
 * 〈一句话功能简述〉<br> 
 * 〈功能详细描述〉
 *
 * @author ryan
 *
 */

@Controller
@RequestMapping("/admin/user")
public class UserManageController {

    @Autowired
    private UserManageService userManageService;

    @NeedAdminLogin
    @RequestMapping(value = "/userlist", method = RequestMethod.GET)
    public String list(Model model) {
        List<Map<String, Object>> users = this.userManageService.listAllUsers();
        model.addAttribute("users", users);

        return "background/user/userlist";
    }
    
    @NeedAdminLogin
    @RequestMapping(value = "/update/{userId}", method = RequestMethod.GET)
    public String updateView(Model model, @PathVariable("userId") Integer userId) {
        
        User user = this.userManageService.getOneUserById(userId);
        model.addAttribute("user", user);
        
        UserRole userRole = this.userManageService.findUserRoleById(userId);
        
        model.addAttribute("userRole", userRole);

        return "background/user/update";
    }
    @NeedAdminLogin
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(User user,@RequestParam("changeRoleName") String changeRoleName) {
        this.userManageService.editUser(user,changeRoleName);

        return "redirect:/admin/user/userlist";
    }
    
    @NeedAdminLogin
    @RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
    public String delete(User user, @PathVariable("userId") Integer userId) {
        this.userManageService.deleteUser(userId);
        return "redirect:/admin/user/userlist";
    }
    
    @NeedAdminLogin
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String addView() {
        return "background/user/add";
    }
    
    @NeedAdminLogin
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(User user, @RequestParam("roleName") String roleName ) {
        this.userManageService.createUser(user, roleName);

        return "redirect:/admin/user/userlist";
    }
    
    @RequestMapping(value="/check",method=RequestMethod.GET)
    public Map<String,Boolean> check(@RequestParam("loginName")String loginName){
        User user=this.userManageService.getOneUserByLoginName(loginName);
        Map<String,Boolean> map=new HashMap<>();
        boolean result;
        if(user==null){
            result=false;
        }else{
            result=true;
        }
        map.put("result", result);
        return map;
    }
    
    
    @NeedAdminLogin
    @RequestMapping(value = "/userlist/{userId}/{userStatus}", method = RequestMethod.GET)
    public String delete(@PathVariable("userId") Integer userId,@PathVariable("userStatus") String userStatus) {
      
        this.userManageService.changeUserStatus(userId, userStatus);
        return "redirect:/admin/user/userlist";
    }
    
}

